$(function ()
{
    findType();
    findAll();
});

function findType()
{
    let data = publivAjax("/back/productTypeList", {}, "get");
    let list = data.list;
    let productType = '<option value="0">全部</option>';
    if (list != null)
    {
        for (let i = 0; i < list.length; i++)
        {
            productType += '<option value="' + list[i].id + '">' + list[i].productType + '</option>';
        }
    }
    $('#productType').html(productType);
}

function findAll()
{
    let msg = {}

    msg.productName = $.trim($("#productName").val());
    msg.enName = $.trim($("#enName").val());
    msg.marketPrice = $.trim($("#marketPrice").val());
    msg.price = $.trim($("#price").val());
    msg.content = $.trim($("#content").val());
    msg.introduction = $("#introduction").val();
    msg.productTypeId = $.trim($("#productType").val());
    msg.order = $.trim($("#order").val());
    msg.lift = $.trim($("#lift").val());
    msg.pageSize = $.trim($("#pageSize").val());
    msg.pageNum = $.trim($("#pageNum").val());
    msg.nowNum = $.trim($("#nowNum").val());

    let data=publivAjax('/back/product/findAll',msg,'get')

    let html = '';
    let list = data.list;
    $("#count").html(data.count);
    if (list != null)
    {
        console.log(list);
        for (let i = 0; i < list.length; i++)
        {
            html += '<tr>\n' +
                '                <td>' + list[i].id + '</td>\n' +
                '                <td>' + list[i].productName + '</td>\n' +
                '                <td>' + list[i].enName + '</td>\n' +
                '                <td>' + list[i].marketPrice + '</td>\n' +
                '                <td>' + list[i].price + '</td>\n' +
                '                <td>' + list[i].content + '</td>\n' +
                '                <td>' + list[i].introduction + '</td>\n' +
                '                <td>' + (list[i].isShow == 1 ? '是' : '否') + '</td>\n' +
                '                <td>' + (list[i].recommend == 1 ? '是' : '否') + '</td>\n' +
                '                <td><img src="' + list[i].img + '" style="width: 70px;height: 50px"></td>\n' +
                '                <td>' + list[i].createTime + '</td>\n' +
                '                <td>' + list[i].updateTime + '</td>\n' +
                '                <td>' + list[i].productTypeModel.productType + '</td>\n' +
                '                <td>\n' +
                '                    <div><a href="javascript:edit(' + list[i].id + ')">编辑</a></div>\n' +
                '                    <div><a href="javascript:del(' + list[i].id + ')">删除</a></div>\n' +
                '                </td>\n' +
                '            </tr>';
        }
    }

    $("#findAll").html(html);
}

function edit(id)
{
    sessionStorage.setItem("productid", id);
    $('#b-right').load('/back/product/productEdit.html')
}

function del(id)
{
    let data = publivAjax('/back/product/del', {id: id}, 'post');
    if (data.result == 1)
    {
        findAll();
    }

}